#ifndef __USER_GAME_H
#define __USER_GAME_H

#include "main.h"
#include "stm32f0xx_hal.h"
#include "userDisplay.h"
#include "settings.c"
#include "useri2cReg.h"

#define load_game (*((void(*)(const char* filename))(0x7ffc/2)))
#define write_flash_page (*((void(*)(const char * page, unsigned char * buffer))(0x7ffa/2)))
#define wrap(i, imax) ((imax+i)%(imax))
#define random(x)    (millis()%x)


#define DIS_STATE_DRAW  0X00
#define DIS_STATE_VER   0X01
#define DIS_STATE_MENU  0X02
#define DIS_STATE_GAME1  0X03
#define DIS_STATE_SAVER 0X04
#define DIS_STATE_GAME2  0X05
#define DIS_STATE_GAME3  0X06

#define GAME_STATE_START        0X01
#define GAME_STATE_RUN          0X02
#define GAME_STATE_GAMEOVER     0X03
#define GAME_STATE_RESTART      0X04
#define GAME_STATE_WIN          0X05




#define MENU_MAX_ITEM    15

class Game{
public:
    Display display;

    void begin();
		int8_t titleScreen(const uint8_t* name, const uint8_t *logo);
		int8_t titleScreen(const uint8_t* name);
		int8_t titleScreenLogo(const uint8_t* logo);
    int8_t titleScreen();
    bool update();
		uint8_t startMenuTimer;
    uint32_t frameCount;
    void setFrameRate(uint8_t fps);
		void pickRandomSeed();
	
		uint8_t getCpuLoad();
    uint16_t getFreeRam();
    uint16_t frameDurationMicros;
    uint32_t frameStartMicros, frameEndMicros;
    
    int8_t menu();
    void keyboard(char* text, uint8_t length);
		uint8_t popupTimeLeft;
    void popup(const uint8_t* text, uint8_t duration);
		//void adjustVolume();
		void changeGame();
		bool settingsAvailable();
		void readSettings();
		void getDefaultName(char* string);
	
		bool collidePointRect(int16_t x1, int16_t y1 ,int16_t x2 ,int16_t y2, int16_t w, int16_t h);
		bool collideRectRect(int16_t x1, int16_t y1, int16_t w1, int16_t h1 ,int16_t x2 ,int16_t y2, int16_t w2, int16_t h2);
    bool collideBitmapBitmap(int16_t x1, int16_t y1, const uint8_t* b1, int16_t x2, int16_t y2, const uint8_t* b2);
	
	
		uint8_t menuItemStr0[15];
		uint8_t menuItemStr1[15];
		uint8_t menuItemStr2[15];
		uint8_t menuItemStr3[15];
		uint8_t menuItemStr4[15];
		uint8_t menuItemStr5[15];
		uint8_t menuItemStr6[15];
		uint8_t menuItemStr7[15];
		uint8_t menuItemStr8[15];
		uint8_t menuItemStr9[15];

		uint8_t *menuItemList[10];
	
		
		uint8_t state;   					//显示模式状态
		uint8_t gameState;        //当前游戏运行状态
		uint8_t gameParameter1;   //游戏参数用来接收开始命令时传递给游戏
		int8_t draw();   					//绘画模式
		int8_t saver();          //屏保模式
		int8_t game1();
		int8_t game2();
		int8_t game3();
		int8_t menuactiveItem;   //指示当前菜单激活item，从0开始
		uint8_t menuItemNumber;   //记录存储的菜单条目数
		void stateCheck();
		
private:
    uint8_t timePerFrame;
    uint32_t nextFrameMillis;
    void updatePopup();
    const uint8_t* popupText;
    void displayBattery();
};


extern Game gb;

#endif

